﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using XiaoYou.Game.Player;
using XiaoYou.Game.Share.Data;
using XiaoYou.Game.Simulator.Leidian;

namespace XiaoYou.Game.Script.SnqxExilium.Player
{
    public class BasePlayer : XiaoYou.Game.Player.ScriptPlayer<LeidianSimulator, LeidianSimulatorInfo>, IPlayer<ScriptPlayerInfo>
    {


        public Lewan lw = new Lewan();


        protected Setting setting;

        public override void Start()
        {
            base.Start(new LeidianSimulator(PlayerInfo.Index, new LeidianSimulatorInfo()
            {
                Cpu = 4,
                Memory = 4096,
                Lockwindow = 1,
                Autorotate = false,
                SimulatorResolution = new Simulator.Adnroid.AndroidSimulatorResolution()
                {
                    Dpi = 160,
                    Height = 540,
                    Weight = 960
                }
            }, "C:\\leidian\\LDPlayer9"));

            Log("关闭模拟器");
            Simulator.Quit();
            lw.Delay(5000);
            Log("开启模拟");
            Simulator.Launch();
            Log("模拟器开启");
            if (lw._lw.GetBindWindow() > 0)
            {
                lw.UnBindWindow();
            }
            lw.BindWindow(Simulator.HandleBind);
            lw.SetPath("images");
            lw.Log = Log;
            if (PlayerInfo.IsTest)
            {
                lw._lw.SetShowErrorMsg(1);
            }
            else
            {
                lw._lw.SetShowErrorMsg(0);
            }
            Log("绑定模拟器");

        }
    
        
        public void GetRegisterAccount()
        {
            Log("获取登陆账号密码");
            try
            {
                string result = new HttpClient().GetStringAsync("http://" + base.PlayerInfo.Address + ":" + base.PlayerInfo.Port + "/getRegister").Result;
                Account = JsonConvert.DeserializeObject<Account>(result);
                Log(Account.LoginName + "---" + Account.Password);
                LogAccount();
            }
            catch (Exception ex)
            {
                Log("获取失败，退出程序-" + ex.Message);
                Environment.Exit(0);
            }
        }
    }

    public class GloPlayer : BasePlayer
    {

        public void EnterAppAndCheck()
        {
            Simulator.KillApp("com.Sunborn.SnqxExilium.Glo");
            lw.Delay(1000);
            Simulator.RunApp("com.Sunborn.SnqxExilium.Glo");
            setting = GetConfig<Setting>().Data;
            CheckEnterApp();
        }

        /// <summary>
        /// 检查是否进入APP
        /// </summary>
        public void CheckEnterApp()
        {
            lw._lw.OpenViolentRedraw(1);
            while (lw.FindPic(824, 18, 903, 84, "游戏打开标记1.bmp", "000000", 0.95f, 0).Success == false)
            {
                Log("判断是否进入游戏");
                lw.Delay(3000);
            }
            Log("成功进入游戏");
            lw.Delay(3000);
        }

        public void Login()
        {
            while (lw.FindPic(32, 175, 52, 201, "账号切换1.bmp", "000000", 0.95f, 0).Error)
            {
                lw.Delay(1000);
                Log("查找账号切换标记");
            }

            Log("找到账号切换标记");

            //点击切换账号
            lw.MoveToClick(30, 175, 55, 200, 1000);//点击切换账号
            lw.MoveToClick(30, 175, 55, 200, 2000);//点击切换账号
            lw.MoveToClick(30, 175, 55, 200);//点击切换账号

            while (lw.FindPic(567, 367, 600, 401, "登录标记散爆*.bmp", "000000", 0.95f, wait: 2000).Error)
            {
                Log("查找散爆登录标记");
            }

            while (lw.FindPic(536, 289, 732, 336, "登录按钮1.bmp", "000000", 0.95f, wait: 2000).Error)
            {
                //点击账户登录
                lw.MoveToClick(574, 369, 654, 396, 2000);//点击切换账号
            }
            InputUserNameAndPassword();

            lw.MoveToClick(540, 294, 726, 328);
        }

        /// <summary>
        /// 输入账号密码
        /// </summary>
        public void InputUserNameAndPassword()
        {
            //Account.LoginName = "shaonv2@hotmail.com";
            //Account.Password = "shaonv123456.";
            //点击账户框
            lw.MoveToClick(421, 195, 579, 215, 2000);
            //for(int i=0;i< 50;i++)
            //{
                lw.KeyPress(39,50,100);
                lw.KeyPress(8, 50,100);
            //}
        
            lw.SendString(Account.LoginName, Mod.m2, wait: 2000);
            //点击密码框
            lw.MoveToClick(423, 240, 622, 259, 2000);
            lw.SendString(Account.Password, Mod.m2, wait: 2000);
        }
    }
}
